import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_staggered_grid_view/flutter_staggered_grid_view.dart';
import 'package:pornhub_app/components/partner_item_cell/partner_item_cell.dart';
import 'package:pornhub_app/model/sys_partner_cell_model/sys_partner_cell_model.dart';
import 'package:pornhub_app/repositories/sys_partner.dart';

class SysPartnerAppListByStationView extends StatefulWidget {
  const SysPartnerAppListByStationView({
    super.key,
    this.title = '',

    /// 0: 列表 1: 宫格
    required this.styleType,
    required this.stationId,
  });

  final String title;

  final int styleType;

  final int stationId;

  @override
  State<SysPartnerAppListByStationView> createState() =>
      _SysPartnerAppListByStationViewState();
}

class _SysPartnerAppListByStationViewState
    extends State<SysPartnerAppListByStationView> {
  List<SysPartnerCellModel> _list = [];

  @override
  void initState() {
    super.initState();
    initList();
  }

  void initList() async {
    var list = await SignInRepositoryImpl().fetchPartnerList(widget.stationId);
    setState(() {
      _list = list;
    });
  }

  Widget _buildTitle() {
    return Row(
      mainAxisAlignment: MainAxisAlignment.start,
      crossAxisAlignment: CrossAxisAlignment.center,
      children: [
        Container(
          decoration: BoxDecoration(
            borderRadius: BorderRadius.circular(2.5),
            color: const Color(0xffdd001b),
          ),
          width: 4.w,
          height: 15.w,
        ),
        SizedBox(
          width: 6.w,
        ),
        Text(
          widget.title,
          style: TextStyle(
            fontSize: 16.w,
            color: const Color(0xff333333),
            letterSpacing: 0,
          ),
        )
      ],
    );
  }

  Widget _buildGridListByStyleTypeOne() {
    return StaggeredGrid.count(
      // padding: const EdgeInsets.all(0),
      mainAxisSpacing: 12.w,
      crossAxisSpacing: 18.w,
      crossAxisCount: 4,
      children: _list
          .map((e) => PartnerItemCell(app: e, style: widget.styleType))
          .toList(),
      // itemCount: _list.length,
      // shrinkWrap: true,
      // itemBuilder: (BuildContext context, int index) {
      //   return PartnerItemCell(app: _list[index], style: widget.styleType);
      // },
    );
  }

  Widget _buildListListByStyleTypeZero() {
    return ListView.separated(
      padding: const EdgeInsets.all(0),
      shrinkWrap: true, // 根据内容大小收缩
      physics: const NeverScrollableScrollPhysics(), // 禁用滚动
      itemCount: _list.length,
      itemBuilder: (context, index) {
        return PartnerItemCell(app: _list[index], style: widget.styleType);
      },
      separatorBuilder: (context, index) {
        return Padding(
          padding: EdgeInsets.symmetric(vertical: 9.w),
          child: const Divider(
            color: Color(0xfff0f0f0),
            height: 1,
          ),
        );
      },
    );
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        if (widget.title.isNotEmpty) _buildTitle(),
        SizedBox(
          height: 12.w,
        ),
        widget.styleType == 0
            ? _buildListListByStyleTypeZero()
            : _buildGridListByStyleTypeOne(),
        SizedBox(
          height: 10.h,
        )
      ],
    );
  }
}
